package com.demomaster.quickcompat.view.menu;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demomaster.quickcompat.R;
import com.demomaster.quickcompat.util.DisplayUtil;
import com.demomaster.quickcompat.widget.base.QuickGravity;
import com.demomaster.quickcompat.widget.popup.QDTipPopupWindow;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;


/**
 * @author squirrel桓
 * @date 2018/11/15.
 * description：
 */
public class OptionsMenu {

    private final WeakReference<Context> contextWeakReference;
    private List<NavMenuItem> menus;
    private OnMenuItemClicked onMenuItemClicked;
    private OptionsMenuAdapter adapter;
    private RecyclerView rcv_options;
    private final int rcv_options_width = -1;
    private View contentView;
    private View anchor;
    private float[] backgroundRadius = new float[8];
    private int dividerColor;
    private boolean withArrow;
    private int arrowWidth;
    private int arrowHeight;
    private int animationStyleID;
    private int textGravity;
    private QuickGravity gravity;

    public OptionsMenu(Builder builder) {
        this.contextWeakReference = builder.contextWeakReference;
        this.gravity = builder.gravity;
        this.backgroundRadius = builder.backgroundRadius;
        this.anchor = builder.anchor;
        this.menus = builder.menus;
        this.textGravity = builder.textGravity;
        this.dividerColor = builder.dividerColor;
        this.arrowHeight = builder.arrowHeight;
        this.arrowWidth = builder.arrowWidth;
        this.withArrow = builder.withArrow;
        this.animationStyleID = builder.animationStyleID;
        init();
    }

   /* public OptionsMenu(final Context context) {
        this.context = context;
        init();
    }*/
    public void init() {
        contentView = LayoutInflater.from(contextWeakReference.get()).inflate(R.layout.quick_dialog_option_menu, null, false);
        rcv_options = contentView.findViewById(R.id.qd_option_menu_recycler);
        rcv_options.setBackgroundColor(Color.TRANSPARENT);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(contextWeakReference.get());
        linearLayoutManager.setAutoMeasureEnabled(true);
        //设置分割线使用的divider
        rcv_options.addItemDecoration(new DividerItemDecoration(contextWeakReference.get(), androidx.recyclerview.widget.DividerItemDecoration.VERTICAL, dividerColor));
        rcv_options.setLayoutManager(linearLayoutManager);

        qdTipPopup = new QDTipPopupWindow.Builder(contextWeakReference.get())
                .setContentView(contentView)
                .setBackgroundRadius(backgroundRadius)
                .setBackgroundColor(rcv_options.getContext().getResources().getColor(R.color.white))
                .setWithArrow(withArrow)
                .setArrowHeight(arrowHeight)
                .setArrowWidth(arrowWidth)
                .setTouchable(true)
                .setAnimationStyleID(animationStyleID)
                .create();
        qdTipPopup.setOnDismissListener(onDismissListener);
        reBuild();
        //popupWindow.setAnimationStyle(R.style.pop_toast);
        //popupWindow.showAtLocation(getContentView(context), Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, dp2px(context,60));
    }

    PopupWindow.OnDismissListener onDismissListener = new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            WindowManager.LayoutParams lp = ((Activity) contextWeakReference.get()).getWindow().getAttributes();
            lp.alpha = 1f;
            ((Activity) contextWeakReference.get()).getWindow().setAttributes(lp);
        }
    };

    private void reBuild() {
        if (menus != null) {
            adapter = new OptionsMenuAdapter(contextWeakReference.get(), menus);
            rcv_options.setAdapter(adapter);
            adapter.setTextGravity(textGravity);
        }
        if (adapter != null) {
            adapter.setOnItemClickListener(onItemClickListener);
        }
    }

    OptionsMenuAdapter.OnItemClickListener onItemClickListener = new OptionsMenuAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position, NavMenuItem menu) {
            if(onMenuItemClicked!=null) {
                onMenuItemClicked.onItemClick(menu, position, null);
                //popupWindow.dismiss();
            }
        }
    };

    private QDTipPopupWindow qdTipPopup;

    public void show() {
        if (anchor != null) {
            WindowManager.LayoutParams lp = ((Activity) contextWeakReference.get()).getWindow().getAttributes();
            //lp.alpha = alpha;
            ((Activity) contextWeakReference.get()).getWindow().setAttributes(lp);
            /*LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) rcv_options.getLayoutParams();
            layoutParams.setMargins(0,0,0,0);
            rcv_options.setLayoutParams(layoutParams);*/
            /*final int anchorLoc[] = new int[2];
             // 获取锚点View在屏幕上的左上角坐标位置
            anchor.getLocationOnScreen(anchorLoc);*/
            //popupWindow.showAsDropDown(anchor);
            //右侧的算法
            //这是旧的处理方式 popupWindow.showAsDropDown(anchor, -rcv_options_width + anchor.getWidth() - margin, margin);
            //popupWindow.showAtLocation(anchor,Gravity.LEFT,anchorLoc);
            qdTipPopup.showTip(anchor, gravity);
        }
    }

    public List getMenus() {
        return menus;
    }

    public void setMenus(List menus) {
        this.menus = menus;
        reBuild();
    }

    public void setMenu(Menu menu){
        this.menus = new ArrayList<>();
        List<NavMenuItem> menus = new ArrayList<>();
        for (int i = 0; i < menu.size(); i++) {
            NavMenuItem menu1 = new NavMenuItem(menu.getItem(i));
            menus.add(menu1);
        }
    }

    public OnMenuItemClicked getOnMenuItemClicked() {
        return onMenuItemClicked;
    }

    public void setOnMenuItemClicked(OnMenuItemClicked onMenuItemClicked) {
        this.onMenuItemClicked = onMenuItemClicked;
        reBuild();
    }

    public void setAnchor(View anchor) {
        this.anchor = anchor;
    }

    public void dismiss() {
        //popupWindow.dismiss();
        if (qdTipPopup != null) {
            qdTipPopup.dismiss();
        }
    }

    public void show(View v) {
        setAnchor(v);
        show();
    }

    public interface OnMenuItemClicked {
        void onItemClick(NavMenuItem menu, int position, View view);
    }

    public static class Builder {
        private WeakReference<Context> contextWeakReference;
        private List<NavMenuItem> menus;
        //private OnMenuItemClicked onMenuItemClicked;
        //private MenuItem.OnMenuItemClickListener onMenuItemClickListener;
        private float alpha = 1;
        private int margin = 4;
        private float[] backgroundRadius = new float[8];
        private float backgroundRadiu;
        private int dividerColor = Color.BLACK;
        private int textGravity = Gravity.CENTER_VERTICAL;
        private boolean withArrow = true;
        private int arrowWidth;
        private int arrowHeight;
        public int animationStyleID = R.style.qd_option_menu_pop_animation;
        private View anchor;
        private QuickGravity gravity = QuickGravity.TOP;

        public Builder setGravity(QuickGravity gravity) {
            this.gravity = gravity;
            return this;
        }

        public Builder(Context context) {
            this.contextWeakReference = new WeakReference<>(context);
            initBuilder();
        }

        public Builder(Context context, List<NavMenuItem> menus) {
            this.contextWeakReference = new WeakReference<>(context);
            this.menus = menus;
            initBuilder();
        }

        private void initBuilder() {
            arrowHeight = DisplayUtil.dip2px(contextWeakReference.get(), 8);
            arrowWidth = DisplayUtil.dip2px(contextWeakReference.get(), 8);
        }

        public Builder setMenus(List<NavMenuItem> menus) {
            this.menus = menus;
            return this;
        }

//        public Builder setOnMenuItemClicked(OnMenuItemClicked onMenuItemClicked) {
//            this.onMenuItemClicked = onMenuItemClicked;
//            return this;
//        }

        public Builder setAlpha(float alpha) {
            this.alpha = alpha;
            return this;
        }

        public Builder setMargin(int margin) {
            this.margin = margin;
            return this;
        }

        public Builder setBackgroundRadius(float backgroundRadiu) {
            this.backgroundRadiu = backgroundRadiu;
            for (int i = 0; i < backgroundRadius.length; i++) {
                this.backgroundRadius[i] = backgroundRadiu;
            }
            return this;
        }

        public Builder setBackgroundRadius(float[] backgroundRadius) {
            this.backgroundRadius = backgroundRadius;
            return this;
        }

        public Builder setAnchor(View anchor) {
            this.anchor = anchor;
            return this;
        }

        public Builder setDividerColor(int dividerColor) {
            this.dividerColor = dividerColor;
            return this;
        }

        public Builder setTextGravity(int textGravity) {
            this.textGravity = textGravity;
            return this;
        }

        public Builder setWithArrow(boolean withArrow) {
            this.withArrow = withArrow;
            return this;
        }

        public Builder setArrowWidth(int arrowWidth) {
            this.arrowWidth = arrowWidth;
            return this;
        }

        public Builder setArrowHeight(int arrowHeight) {
            this.arrowHeight = arrowHeight;
            return this;
        }

        public Builder setAnimationStyleID(int animationStyleID) {
            this.animationStyleID = animationStyleID;
            return this;
        }

        public OptionsMenu creat() {
            return new OptionsMenu(this);
        }

    }

}
